﻿using System.Data;
using System.Data.OleDb;
using TraditionalMalteseCuisine.App_Code.Objects;

namespace TraditionalMalteseCuisine.App_Code.Operations
{
    public class ReadOp : ConnectionOp
    {
        //*************************************************
        //                   Methods
        //*************************************************
        #region OleDb Methods
        /// <summary>
        /// Check whether a table name is present in a database file.
        /// </summary>
        /// <param name="table_name">The table name to check for.</param>
        /// <returns>True if table name exists, otherwise False.</returns>
        public static bool DoesTableNameExists(string table_name)
        {
            bool exists = false;

            DataTable dt = GetOleDbSchemaTable();

            foreach (DataRow dr in dt.Rows)
            {
                if (dr["table_name"].ToString().ToLower().Contains(table_name.ToLower()))
                {
                    exists = true;
                    break;
                }
            }

            return exists;
        }


        /// <summary>
        /// Get a DataTable filled with the table names inside a database file.
        /// </summary>
        /// <returns>A DataTable filled with the table names of the database file.</returns>
        public static DataTable GetOleDbSchemaTable()
        {
            DataTable dt = null;

            try
            {
                if (OpenOleDbConnection())
                    dt = OleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            }
            finally
            {
                CloseOleDbConnection();
            }

            return dt;
        }


        /// <summary>
        /// Get a DataTable filled with the result of an OleDb command.
        /// </summary>
        /// <param name="commandText">The OleDb command for the data table.</param>
        /// <returns>A DataTable filled with the result of the OleDb command.</returns>
        public static DataTable GetDataTable(string commandText)
        {
            return GetDataTable(new OleDbCommand(commandText));
        }

        /// <summary>
        /// Get a DataTable filled with the result of an OleDb command.
        /// </summary>
        /// <param name="oledbCommand">The OleDb command for the data table.</param>
        /// <returns>A DataTable filled with the result of the OleDb command.</returns>
        public static DataTable GetDataTable(OleDbCommand oledbCommand)
        {
            DataTable dt = null;

            OleDbDataAdapter da = GetDataAdapter(oledbCommand);

            if (da != null)
            {
                dt = new DataTable();

                da.Fill(dt);
            }

            return dt;
        }


        /// <summary>
        /// Get an OleDbDataAdapter with an OleDb command assigned.
        /// </summary>
        /// <param name="oledbCommand">The OleDb command which the data adapter has to use.</param>
        /// <returns>An OleDbDataAdapter with the OleDb command assigned.</returns>
        public static OleDbDataAdapter GetDataAdapter(OleDbCommand oledbCommand)
        {
            OleDbDataAdapter da = null;

            try
            {
                if (OpenOleDbConnection())
                {
                    oledbCommand.CommandTimeout = 0;
                    oledbCommand.Connection = OleDbConnection;
                    da = new OleDbDataAdapter(oledbCommand);
                }
            }
            finally
            {
                CloseOleDbConnection();
            }

            return da;
        }


        /// <summary>
        /// Get a single Int32 value from the OleDb command text.
        /// </summary>
        /// <param name="commandText">The OleDb command to execute.</param>
        /// <returns>The result of the OleDb command.</returns>
        public static int GetIntResult(string commandText)
        {
            return WriteOp.ExecuteWithIntResult(commandText);
        }

        /// <summary>
        /// Get a single Int32 value from the OleDb command.
        /// </summary>
        /// <param name="oledbCommand">The OleDb command to execute.</param>
        /// <returns>The result of the OleDb command.</returns>
        public static int GetIntResult(OleDbCommand oledbCommand)
        {
            return WriteOp.ExecuteWithIntResult(oledbCommand);
        }


        /// <summary>
        /// Get a single String value from the OleDb command text.
        /// </summary>
        /// <param name="commandText">The OleDb command to execute.</param>
        /// <returns>The result of the OleDb command.</returns>
        public static string GetStringResult(string commandText)
        {
            return WriteOp.ExecuteWithStringResult(commandText);
        }

        /// <summary>
        /// Get a single String value from the OleDb command.
        /// </summary>
        /// <param name="oledbCommand">The OleDb command to execute.</param>
        /// <returns>The result of the OleDb command.</returns>
        public static string GetStringResult(OleDbCommand oledbCommand)
        {
            return WriteOp.ExecuteWithStringResult(oledbCommand);
        }
        #endregion
        //*************************************************
    }
}